CAST128
This algorithm is an implementation of CAST-128, a 64-bit block cipher with a variable length key from 8 to 128 bits. The algorithm is described in RFC-2144.
CAST128 cipher initialization
This cipher supports both ECB and CBC modes, and can be used with NoPadding or PKCS5Padding. To create an instance of this class, use the Cipher.getInstance() method with “SAFENET” as the provider and one of the following strings as the transformation:
-
CAST128
-
CAST128/ECB/NoPadding
-
CAST128/ECB/PKCS5Padding
-
CAST128/CBC/NoPadding
-
CAST128/CBC/PKCS5Padding
Using the “CAST128” transformation, the Cipher will default to ECB and NoPadding.
If the NoPadding mode is selected, the input data must be a multiple of 8 bytes; otherwise, the encrypted or decrypted result will be truncated. In PKCS5Padding, arbitrary data lengths are accepted; the ciphertext will be padded to a multiple of 8 bytes, as described in PKCS#5. The decryption process will remove the padding from the data so that the correct plaintext is returned.
This Cipher will accept a javax.crypto.spec.SecretKeySpec or au.com.safenet.crypto.provider.CryptokiSecretKey as the key parameter during initialization.
When the Cipher is initialized in CBC mode, the Initialization Vector (IV) can be specified by passing a javax.crypto.spec.IvParameterSpec instance to the Cipher.init() method. When decrypting in this mode, a valid IV must be specified in the Cipher.init() method. For encryption, however, a random IV will be generated if none is specified (the IV can be retrieved using the Cipher.getIV() method).
The IV can be provided as a java.security.AlgorithmParameters or a javax.crypto.spec.IvParameterSpec instance. If the initialization is done using an AlgorithmParameters instance, it must be convertible to an IvParameterSpec using the AlgorithmParameters.getParameterSpec() method.
This Cipher does not support the Cipher.getParameters() method; this method will always return null
. The only supported parameter for this class is the initialization vector, which can be determined using the Cipher.getIV() method.
CAST128 key
The CAST128 Cipher requires either a SecretKeySpec or ProtectToolkit-J provider CAST128 key during initialization. The CAST128 key may be any length of 8 to 128 bits.
To create an appropriate SecretKeySpec, pass an array of up to 16 bytes and the algorithm name “CAST128” to the SecretKeySpec constructor. For example:
byte[] keyBytes = { 0x41, 0x22, 0x35, 0x17,
0x39, 0xDF, 0x28, 0x94,
0x11, 0x93, 0x55, 0x67,
0x39, 0xAC, 0xCD, 0xFF };
SecretKeySpec castKey = new SecretKeySpec(keyBytes,
“CAST128”);
Alternatively, a random ProtectToolkit-J CAST128 key can be generated using the KeyGenerator as described in Key generation, or, a provider-independent form. The CAST128 key can also be stored in the ProtectToolkit-J KeyStore as described in Key storage.
The ProtectToolkit-J CAST128 key will return the string “CAST128” as its algorithm name, “RAW” as its encoding. However, since the key is stored within the hardware, the actual key encoding may not be available.
The key value can only be extracted from a key if the associated Cryptoki key is not marked as Sensitive. The keys generated in ProtectToolkit-J will always be marked as sensitive. It is possible, however, to access any Cryptoki keys stored on the device, and it is possible that the attributes of these keys have been modified.
CAST128 KeyGenerator
The CAST128 KeyGenerator is used to generate random CAST128 keys. The generated key will be a hardware key that has the Cryptoki CKA_EXTRACTABLE and CKA_SENSITIVE attributes set. Since these keys are marked as sensitive, their getEncoded() method will return null
.
During initialization, the strength parameter may be any length from 8 to 128. The default key size is 128 bits. The random parameter is ignored as the hardware includes a cryptographically-secure random source.
Keys generated using the KeyGenerator are not thread-safe. That is, a ProtectToolkit-J Key instance can only be used by a single Cipher instance (as well as a single MAC instance) at any given time. See Key generation for information on threading and ProtectToolkit-J keys.
CAST128 SecretKeyFactory
The CAST128 SecretKeyFactory is used to construct ProtectToolkit-J keys from their provider-independent form. The provider-independent form of the CAST128 key is the au.com.safenet.crypto.spec.CASTKeySpec class.
Keys generated using the SecretKeyFactory are not thread-safe. That is, a ProtectToolkit-J Key instance can only be used by a single Cipher instance (as well as a single MAC instance) at any given time. See Key generation for information on threading and ProtectToolkit-J keys.
For example, to create the provider-based key from its provider-independent form:
byte[] keyBytes = { 0x41, 0x22, 0x35, 0x17,
0x39, 0xDB, 0xDC, 0xEF
0x11, 0x93, 0x55, 0x67,
0x39, 0xAC, 0xCD, 0xFF };
CAST128KeySpec castKeySpec = new CAST128KeySpec(keyBytes);
SecretKeyFactory castKeyFact =
SecretKeyFactory.getInstance(“CAST128”, “SAFENET”);
SecretKey castKey=castKeyFact.generateSecret(castEdeKeySpec);
CAST128 example code
See DES example code for the simple DES example. To convert the example to use CAST128, use “CAST128” in place of “DES”.